import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBProjectPopPage/YBZuZhiGuanLiQuanXianPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineAuthVipModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineMainPage/YBMineScrollInfoEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineProvider/YBMineUserProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineSubPage/YBMineExlistHeightPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineSubPage/YBMineSectionPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineSubPage/YBMineSubAppBarPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBCallPhoneUtils.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBisLoginUtils.dart';

class YBMineMainPage extends StatefulWidget {
  @override
  _YBMineMainPageState createState() => _YBMineMainPageState();
}

class _YBMineMainPageState extends State<YBMineMainPage> {

  ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      setState(() {
        mineScrollInfoEventBus.fire(YBMineScrollInfoEventBus(scrollInfo:_scrollController.offset.toInt()));
      });
    });
  }


  @override
  Widget build(BuildContext context) {
    return Container(
        color: YBColor.commonPageBackGroundColor,
        child: Scaffold(
          body: SafeArea(
              child:Padding(
                padding:EdgeInsets.only(left: 0,right: 0),
                child: ChangeNotifierProvider(
                  create:(context) => YBMineUserProvider()..getUserInfoData(),
                  child: Consumer<YBMineUserProvider>(
                      builder: (context,userModel,child){
                        String _authStr = YBChineseText.mineUnAuthTitle;
                        if (userModel.mineUserInfoModel != null){
                          if (userModel.mineUserInfoModel.certStatus != 2){
                            _authStr = YBChineseText.mineUnAuthTitle;
                          }else{
                            _authStr = YBChineseText.mineAuthTitle;
                          }
                        }

                        if (userModel.mineUserInfoModel != null){
                          if (userModel.mineUserInfoModel.tagCodeSet != null){
                            if (userModel.mineUserInfoModel.tagCodeSet.contains("cow_lawyer") || userModel.mineUserInfoModel.tagCodeSet.contains("real_cow_lawyer") ||
                                userModel.mineUserInfoModel.tagCodeSet.contains("good_select") || userModel.mineUserInfoModel.tagCodeSet.contains("real_good_select")){
                              SpUtil.putString("MIShuTong", "1");
                            }
                          }

                          if (userModel.mineUserInfoModel.customerAuthorities != null) {
                            for (YBMineAuthVipModel mineAuthModel in userModel.mineUserInfoModel
                                .customerAuthorities) {
                              if (mineAuthModel.authority.contains('BLVIP')) {
                                SpUtil.putString("VIP", "1");
                              }else if (mineAuthModel.authority.contains("STORE")){
                                SpUtil.putString("STORE", "1");
                              }else if (mineAuthModel.authority.contains("LAWYER")){
                                SpUtil.putString("LAWYER", "1");
                              }
                            }
                          }
                        }

                        return SmartRefresher(
                          controller:userModel.easyRefreshController,
                          enablePullUp: true,
                          enablePullDown: true,
                          onRefresh: () async {
                            userModel.getUserInfoData();
                          },
                          child: CustomScrollView(
                            controller: _scrollController,
                            slivers: [
                              YBMineSubAppBarPage(userInfoModel: userModel.mineUserInfoModel??YBMineUserInfoModel(),),
                              YBMineExListHeightPage(),
                              SliverToBoxAdapter(
                                child: Container(
                                  color: YBColor.colorF7F8FA,
                                  child: Padding(
                                    padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20,top: YBTextStyle.adapterSize16),
                                    child:Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(YBTextStyle.adapterSize8),
                                        color: YBColor.white,
                                      ),
                                      child: Padding(
                                        padding: EdgeInsets.only(left: YBTextStyle.adapterSize16,right: YBTextStyle.adapterSize12),
                                        child: Column(
                                          children: [
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minezuzhiganli",titleStr: YBChineseText.mineZuZhiManagerTitle,),
                                              onTap: (){
                                                if(YBisLoginUtils.isLogin() == true){
                                                  if(userModel.mineUserInfoModel.certStatus == 2){
                                                    YBNavigatorUtil.jump(context, "/mine/mineZhuZhiManagerPage");
                                                  }else{
                                                    YBZuZhiGuanLiQuanXianPage.showZuZhiGuanLiQuanXian(context);
                                                  }
                                                }else{
                                                  YBApplication.router.navigateTo(context, '/loginIn');
                                                }
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minezhangshanggonghui",titleStr: YBChineseText.mineYuanShiGongHuiTitle,subTitleStr:YBChineseText.mineBanMaiShengQianTitle,),
                                              onTap: (){
                                                YBNavigatorUtil.jump(context, "/mine/mineYuanShiGongHuiPage");
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minewodequanyi",titleStr: YBChineseText.mineMyQuanYiTitle,),
                                              onTap: (){
                                                if(YBisLoginUtils.isLogin() == true){
                                                  YBNavigatorUtil.jump(context, "/mine/mineMyQuanYiPage");
                                                }else{
                                                  YBApplication.router.navigateTo(context, '/loginIn');
                                                }
                                              },
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              SliverToBoxAdapter(
                                child: Container(
                                  color: YBColor.colorF7F8FA,
                                  child: Padding(
                                    padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20,top: YBTextStyle.adapterSize16,bottom: YBTextStyle.adapterSize20),
                                    child: Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(YBTextStyle.adapterSize8),
                                        color: YBColor.white,
                                      ),
                                      child: Padding(
                                        padding:EdgeInsets.only(left: YBTextStyle.adapterSize16,right: YBTextStyle.adapterSize12),
                                        child: Column(
                                          children: [
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "mineshimingrenzheng",titleStr: YBChineseText.mineShiMingAuthTitle,subTitleStr: _authStr,),
                                              onTap: (){
                                                if(YBisLoginUtils.isLogin() == true){
                                                  if (userModel.mineUserInfoModel.certStatus == -1 || userModel.mineUserInfoModel.certStatus == 1 || userModel.mineUserInfoModel.certStatus == 2) {
                                                    if (userModel.mineUserInfoModel.customerType == 1) {
                                                      YBNavigatorUtil.jump(context, '/mine/mineQiYeDisPlayPage');
                                                    } else if (userModel.mineUserInfoModel.customerType == 2) {
                                                      YBNavigatorUtil.jump(context, '/mine/minePersonDisPlayPage');
                                                    }
                                                  }else{
                                                    YBNavigatorUtil.jump(context, '/mine/mineQiYePersonPage');
                                                  }
                                                }else{
                                                  YBApplication.router.navigateTo(context, '/loginIn');
                                                }
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minewodekefu",titleStr: YBChineseText.mineMyDeKeFuTitle,),
                                              onTap: (){
                                                YBCallPhoneUtils().getCallPhoneUtils('tel:4008260189');
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minewoyaohezuo",titleStr: YBChineseText.mineMyDeHeZuoTitle,),
                                              onTap: (){
                                                YBCallPhoneUtils().getCallPhoneUtils('tel:4008260189');
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "minebangzhuyufankui",titleStr: YBChineseText.mineBindingYuFanKuiTitle,),
                                              onTap: (){
                                                YBNavigatorUtil.jump(context, "/baseWebView?type=3");
                                              },
                                            ),
                                            InkWell(
                                              child: YBMineSectionPge(imageStr: "mineaboutus",titleStr: YBChineseText.mineUsAboutTitle),
                                              onTap: (){
                                                YBNavigatorUtil.jump(context, '/mine/mineAboutUsPage');
                                              },
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      }
                  ),
                ),
              ),
          ),
        ),
    );
  }
}
